<?php
class Reale_Model_Estate_Project extends Reale_Model_Estate_Generic
{
    /**
     * 
     * Stores project's name
     * @var string
     */
    protected $_project_name;
    /**
     * 
     * Stores project's area covered
     * @var double
     */
    protected $_total_area;
    /**
     * Property Specification
     * @var array|Reale_Model_Estate_Spec
     */
    protected $_spec;
    
    /**
     * 
     * Enter description here ...
     * @var array|Reale_Model_Estate_Brokerage
     */
    protected $_brokerage;
    /**
     * 
     * Stores enquiry entity mapper
     * @var Reale_Model_ProjectMapper
     * 
     */
    protected $_mapper;
    /**
     * Constructor
     * 
     * @param  array|null $options 
     * @return void
     */
    public function __construct (array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }
    
    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @param  mixed $value 
     * @return void
     */
    public function __set ($name, $value)
    {
        $method = 'set' . $name;
        if ('mapper' == $name || ! method_exists($this, $method)) {
            throw new Exception('Invalid property specified', Zend_Log::DEBUG);
        }
        $this->$method($value);
    }
    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @return mixed
     */
    public function __get ($name)
    {
        $method = 'get' . $name;
        if ('mapper' == $name || ! method_exists($this, $method)) {
            throw new Exception('Invalid property specified', Zend_Log::DEBUG);
        }
        return $this->$method();
    }
    /**
     * Set object state
     * 
     * @param  array $options 
     * @return Reale_Model_Estate_Property
     */
    public function setOptions (array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    /**
     * Set object to array
     * 
     * @return array
     */
    public function __toArray ()
    {
        $data = array();
        $methods = get_class_methods($this);
        foreach ($methods as $key => $method) {
            if ('get' == substr($method, 0, 3)) {
                $output = $this->$method();
                if (is_array($output) || is_string($output)) {
                    $newKey = strtolower(substr($method, 3));
                    if (('getSpec' == $method or 'getBrokerage' == $method) and is_array($output)) {
                        continue;
                    } else {
                        $data[$newKey] = $output;
                    }
                }
            }
            ;
        }
        return $data;
    }
    /**
     * 
     * Setter function for project name
     * @param string project_name 
     * @return Reale_Model_Estate_Project
     */
    public function setProject_name ($project_name)
    {
        $this->_project_name = $project_name;
        return $this;
    }
    /**
     * 
     * Getter function for project name
     * @return string project_name
     */
    public function getProject_name ()
    {
        return $this->_project_name;
    }
    /**
     * @return the $_total_area
     */
    public function getTotal_area ()
    {
        return $this->_total_area;
    }
    /**
     * @param double $_total_area
     * @return Reale_Model_Estate_Project
     */
    public function setTotal_area ($_total_area)
    {
        $this->_total_area = $_total_area;
        return $this;
    }
    /**
     * @return Reale_Model_Estate_Spec $_spec
     */
    public function getSpec ()
    {
        if (null === $this->_spec) {
            $this->setSpec(new Reale_Model_Estate_Spec());
        }
        return $this->_spec;
    }
    /**
     * @param Reale_Model_Estate_Spec $_spec
     * @return Reale_Model_Estate_Project
     */
    public function setSpec (Reale_Model_Estate_Spec $_spec)
    {
        $this->_spec = $_spec;
        return $this;
    }

    /**
     * If multiple specifications are given. It can be set as array.
     * 
     * @param array $specValues
     * @return array Reale_Model_Estate_Spec
     */
    public function setSpecValues (array $specValues)
    {
        foreach ($specValues as $key => $specLine) {
            $spec = array();
            $specLineArray = explode(';', $specLine);
            foreach ($specLineArray as $key => $specLinePair) {
                $specPair = explode(':', $specLinePair);
                $spec[$specPair[0]] = $specPair[1];
            }
            /**
             * Converting the given size in default unit size.
             */
            if (!isset($spec['area_unit'])) {
                $spec['area_unit'] = 'sqft';
            }
            if (isset($spec['size']) and 'sqft' != $spec['area_unit']) {
                $spec['size'] = Reale_Controller_Helper_UnitConverter::direct($spec['size'], $spec['area_unit']);
                $unit = Reale_Controller_Helper_UnitConverter::direct(1,'sqft',$spec['area_unit']);
                $spec['rate_per_unit'] = $spec['rate_per_unit']*$unit;
            }
            $spec['input_unit'] = $spec['area_unit'];
            unset($spec['area_unit']);
            $this->_spec[] = new Reale_Model_Estate_Spec($spec);
        }
    }
    /**
     * @return array|Reale_Model_Estate_Brokerage $_brokerage
     */
    public function getBrokerage ()
    {
        return $this->_brokerage;
    }

	/**
     * @param Reale_Model_Estate_Brokerage $_brokerage
     */
    public function setBrokerage (Reale_Model_Estate_Brokerage $_brokerage)
    {
        $this->_brokerage = $_brokerage;
    }

    
	/**
     * @param array|Reale_Model_Estate_Brokerage $_brokerage
     */
    public function setBrokerageValues (array $brokerageValues)
    {
        foreach ($brokerageValues as $key => $brokerageLine) {
            $brokerage = array();
            $brokeLineArray = explode(';', $brokerageLine);
            foreach ($brokeLineArray as $key => $brokeLinePair) {
                $brokeragePair = explode(':', $brokeLinePair);
                $brokerage[$brokeragePair[0]] = $brokeragePair[1];
            }
            $this->_brokerage[] = new Reale_Model_Estate_Brokerage($brokerage);
        }
    }
    
	/**
     * 
     * Setter function for project mapper 
     * @param Reale_Model_Estate_ProjectMapper
     */
    public function setMapper (Reale_Model_Estate_ProjectMapper $mapper)
    {
        $this->_mapper = $mapper;
        return $this;
    }
    /**
     * 
     * Getter function for enquiry mapper
     * @return Reale_Model_Estate_ProjectMapper $_mapper
     */
    public function getMapper ()
    {
        if (null === $this->_mapper) {
            $this->setMapper(new Reale_Model_Estate_ProjectMapper());
        }
        return $this->_mapper;
    }
    /**
     * 
     * To save new or update existing project's informtion.
     * return bool
     * 
     */
    public function save ()
    {
        $return = $this->getMapper()->save($this);
        if ($return) {
            /* I think not need as mapper accepts &$this
        	if ($return instanceof Reale_Model_Estate_Property) {
        		$this->setEstate_id($return->getEstate_id());
        	}*/
            return true;
        }
        return null;
    }
    /**
     * 
     * Getter function for data member estate_type
     * @return string $_estate_type
     */
    public function getEstate_Type ()
    {
        if (! isset($this->_estate_type)) {
            self::setEstate_Type();
        }
        return $this->_estate_type;
         //throw new Exception('The objects primary key(i.e. $_estate_type) is not set.',Zend_Log::ERR); 
    }
    
    /**
     * 
     * Setter function for data member estate_type
     * @param Reale_Model_Estate_Project
     */
    public function setEstate_Type ($estate_type = NULL)
    {
        parent::setEstate_Type('PROJECT');
    }
}

